package com.accp.taobao.util;

import com.accp.taobao.service.ReadFileThread;

import java.io.*;
import java.util.HashMap;

public class ReadFileInfo {
    //一次性读入文本的工具
    public static StringBuffer getRowData(String filepath){
        HashMap<Integer,String> datagroups=new HashMap<>();
        File file=new File(filepath);
        int interval = (int)(file.length()/10);
        for (int i = 0; i < 10; i++) {
            datagroups.put(i,null);
            Thread readThread=new ReadFileThread(file,i,interval,datagroups);
            readThread.start();
        }
        //休眠，直到所有读文本的子线程结束任务
        try {
            while(Thread.activeCount()>2){
                Thread.sleep(0);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        StringBuffer rowData=new StringBuffer();
        for (int i = 0; i < datagroups.size(); i++) {
            rowData.append(datagroups.get(i));
        }
        return rowData;
    }
}
